package com.jiangtour.service.impl;

import com.jiangtour.dataobject.ConfigureCategory;
import com.jiangtour.repository.ConfigureCategoryRepository;
import com.jiangtour.service.ConfigureCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @Title: ConfigureCategoryServiceImpl
 * @ProjectName site
 * @Description: TODO
 * @date 2018/12/24/02417:07
 */
@Service
public class ConfigureCategoryServiceImpl implements ConfigureCategoryService {

    @Autowired
    private ConfigureCategoryRepository repository;

    @Override
    public ConfigureCategory findOne(Integer categoryId) {
        return repository.findById(categoryId).get();
    }

    @Override
    public ConfigureCategory save(ConfigureCategory configureCategory) {
        return repository.save(configureCategory);
    }

    @Override
    public List<ConfigureCategory> findList(String categoryAscription,Integer type) {

        List<ConfigureCategory> all = repository.findAll(new Specification<ConfigureCategory>() {
            @Override
            public Predicate toPredicate(Root<ConfigureCategory> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                predicate.getExpressions().add(criteriaBuilder.equal(root.get("categoryAscription"), categoryAscription));
                predicate.getExpressions().add(criteriaBuilder.equal(root.get("categoryType"), type));
                return predicate;
            }
        });


        return all;
    }
}
